using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ENTITIES;
using BUS;
using Utility;

public partial class admin_DanhSachKhachHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
    }
    private void LoadData()
    {
        PagerV2_8_1.PageSize = 3;
        Criteria a = new Criteria();
        if (txt_MaKH.Text != "")
        {
            a.add(Condition.AND, tbKhachHangColumns.MaKH, Condition.LIKE, txt_MaKH.Text);
        }
        if (txt_TenKH.Text != "")
        {
            a.add(Condition.AND, tbKhachHangColumns.TenKH, Condition.LIKE, txt_TenKH.Text);
        }

        int total = 0;
        tbKhachHangCollection khachhangcollection =
          KhachHangBUS.Get(a.Criter, PagerV2_8_1.CurrentIndex, PagerV2_8_1.PageSize,
          tbKhachHangColumns.MaKH.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = khachhangcollection;
        Repeater1.DataBind();
        PagerV2_8_1.ItemCount = total;
        
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                TBKhachHang khachhang = KhachHangBUS.GetByID(id);

                KhachHangBUS.Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
        e.Item.ItemType == ListItemType.Item)
        {
            TBKhachHang khachhang = (TBKhachHang)e.Item.DataItem;
            HyperLink hyp_TenKH = (HyperLink)e.Item.FindControl("hyp_TenKH");

            hyp_TenKH.Text = khachhang.tenkh;
            hyp_TenKH.NavigateUrl = "~/admin/ChiTietKhachHang.aspx?id=" + khachhang.makh.ToString();

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/admin/SuaKhachHang.aspx?id=" + khachhang.makh.ToString();
        }
    }
  
    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        LoadData();
    }
 
    protected void PagerV2_8_1_Command(object sender, CommandEventArgs e)
    {
        PagerV2_8_1.CurrentIndex = int.Parse(e.CommandArgument.ToString());
         LoadData();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
}
